create enum

Define a new enumeration

Syntax 

create_enum:
        CREATE ENUM enum_name AS (constant_name [= value] [, constant_name [= value]] ...)

Description

The CREATE ENUM statement is used to define an enumeration ("ENUM") for an RaimaDB database. ENUMs can be used as a data type of a column in a table or in a user-defined type.

An ENUM must include a list of enum constants in the CREATE ENUM statement. Each enum constant can include the corresponding numeric value.

Using an ENUM as a column's data type ensures the column only accepts a set of values defined in those ENUMs. If an application attempts to insert into an ENUM column a value that does not correspond to any value in the ENUM, RaimaDB returns an "Invalid ENUM value" error (eINVENUMVALUE).

Components

The CREATE ENUM statement includes the following components:

enum_name Name of the enumeration
constant_name Constant that represents a numeric value.
value The value that corresponds to the enum constant. It must fit within a signed 32-bit integer.

Examples

-- create an enum that represents colours
CREATE ENUM my_color AS (red, blue, green, yellow, orange);

-- create an enum that represents shapes with explicit values
CREATE ENUM my_shape AS (
    "Triangle" = 1,
    "Square" = 2,
    "Pentagon" = 3,
    "Hexagon" = 4,
    "Circle" = 100
);

-- create a table using an ENUM as a column data type
CREATE TABLE test_table (
    test_id ROWID PRIMARY KEY,
    Test_shape MY_SHAPE KEY NOT NULL
);

Comments

The ENUM defined with the CREATE ENUM statement is typedef'd as a C-style enum declaration in the C/C++ header file generated by rdm-compile. For example, the ENUMs defined above (see "Examples") are declared as follows in the generated header file:

/** \brief type declaration for enum MY_COLOR */
typedef enum
{
    RED,
    BLUE,
    GREEN,
    YELLOW,
    ORANGE
} MY_COLOR;

/** \brief type declaration for enum MY_SHAPE */
typedef enum
{
    Triangle = 1,
    Square = 2,
    Pentagon = 3,
    Hexagon = 4,
    Circle = 100
} MY_SHAPE;

The C standard does not specify the data type that represents the enumerated type. It means an enumerated type in C may be a signed 32-bit integer, an unsigned 32-bit integer, a signed 64-bit integer, etc, depending on the platform and/or compiler. In RaimaDB, the ENUM created with CREATE ENUM is represented as a signed 32-bit integer (INT32) in the database. Because of this, care must be taken to make sure the values specified with ENUMs fit within a signed 32-bit integer.

The column defined as ENUM can use one of the constants defined in the ENUM as the default value. The following example uses the constant GREEN as the default value of the test_color column, which is of type MY_COLOR.

-- create a table using an ENUM as a column data type
CREATE TABLE test_table (
    test_id ROWID PRIMARY KEY,
    test_colour MY_COLOR NOT NULL DEFAULT green
);

The corresponding numeric value can also be used as the column default value. However, if the numeric value does not have the corresponding constant in the ENUM, RaimaDB returns an "Invalid default specification for the type" error (eDDL_INVDEF).

See Also

CREATE TABLE

CREATE TYPE